<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="教师名字" prop="name">
      <el-select v-model="dataForm.name" placeholder="请选择" @focus="selectTeacher">
      <el-option
        v-for="(item, index) in teachers"
        :key="item.id"
        :label="item.name"
        @click.native ="changeTeacher(index)"
        :value="item.id">
      </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="课题" prop="subject">
      <el-input v-model="dataForm.subject" placeholder="课题"></el-input>
    </el-form-item>
    <el-form-item label="联系方式" prop="phone">
      <el-input :disabled="true" v-model="dataForm.phone" placeholder="联系方式"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    props: ['type'],
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          subject: '',
          phone: '',
          createTime: '',
          updateTime: '',
          isDelete: ''
        },
        teachers: [],
        dataRule: {
          name: [
            { required: true, message: '教师名字不能为空', trigger: 'blur' }
          ],
          subject: [
            { required: true, message: '课题不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '联系方式不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '是否删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      changeTeacher(index){
        this.dataForm = this.teachers[index]
        console.log(this.dataForm)
      },
      selectTeacher(){
        this.$http({
              url: this.$http.adornUrl(`/edm/teacher/listNoPage`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.teachers = data.teachers
              }
            })
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/gdm/teacher/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.teacher.name
                this.dataForm.phone = data.teacher.phone
                this.dataForm.subject = data.teacher.subject
                this.dataForm.createTime = data.teacher.createTime
                this.dataForm.updateTime = data.teacher.updateTime
                this.dataForm.isDelete = data.teacher.isDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/gdm/teacher/${this.type == 0 ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'subject': this.dataForm.subject,
                'phone': this.dataForm.phone,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'isDelete': this.dataForm.isDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
